const { app, BrowserWindow, ipcMain, screen } = require("electron");
const path = require("node:path");
//是否是测试环境
const isDevelopment = !app.isPackaged;


module.exports = {
    AdsorbWindow: function (w, h, hash) {
        /*========================================>( 方法 )<========================================*/
        //判断右侧是否出界
        const isRightOutOfDesk = () => {
            const { width: screenWidth } = screen.getPrimaryDisplay().workAreaSize;
            const { x, width } = window.getBounds();
            const { width: spaceWidth } = this.adsorbWin.getBounds()
            const rightX = x + width + spaceWidth
            if (rightX > screenWidth) {
                return true
            }
            return false
        }

        //判断左侧是否出界
        const isLeftOutOfDesk = () => {
            const { x } = window.getBounds();
            const { width: spaceWidth } = this.adsorbWin.getBounds()
            const rightX = x - spaceWidth
            if (rightX < 0) {
                return true
            }
            return false
        }

        //右外吸附
        const absorbRight = () => {
            const { x: parentX, y: parentY, width: parentWidth } = momentParentBounds
            const finalX = parentX + parentWidth
            const finalY = parentY
            this.adsorbWin.setPosition(finalX, finalY);
            adsorption = 'r'
        }
        //左外吸附
        const absorbLeft = () => {
            const { x: parentX, y: parentY } = momentParentBounds
            const { width } = this.adsorbWin.getBounds()
            const finalX = parentX - width
            const finalY = parentY
            this.adsorbWin.setPosition(finalX, finalY);
            adsorption = 'l'
        }
        //右内吸附
        const absorbInnerRight = () => {
            const { x: parentX, y: parentY, width: parentWidth, height: parentHeight } = momentParentBounds
            const { width, height } = this.adsorbWin.getBounds()
            const finalX = parentX + parentWidth - width
            const finalY = parentY + Math.floor((parentHeight - height) / 2)
            this.adsorbWin.setPosition(finalX, finalY);
            adsorption = 'nr'
        }
        //左内吸附
        const absorbInnerLeft = () => {
            const { x: parentX, y: parentY, height: parentHeight } = momentParentBounds
            const { height } = this.adsorbWin.getBounds()
            const finalX = parentX
            const finalY = parentY + Math.floor((parentHeight - height) / 2)
            this.adsorbWin.setPosition(finalX, finalY);
            adsorption = 'nl'
        }


        const window = BrowserWindow.getFocusedWindow();
        //本模块上一时刻位置Bounds
        let momentParentBounds = window.getBounds()
        //吸附状态
        let adsorption = undefined
        //创建窗口
        this.adsorbWin = new BrowserWindow({
            width: w,
            height: h,
            show: false,
            backgroundColor: "#00000000",
            titleBarStyle: "hidden",
            frame: false,
            resizable: false,
            hasShadow: false,
            parent: window,
            webPreferences: {
                preload: path.join(__dirname, "../preload.js"),
            }
        });
        //判断初始位置
        //1.如果主应用全屏，则默认右内测 
        //2.如果主应用边距不足，则默认右内测
        //3.其余情况，右内测
        if (window.isMaximized() || isRightOutOfDesk()) {
            absorbInnerRight()
        } else {
            absorbRight()
        }
        // this.adsorbWin.webContents.openDevTools();
        this.adsorbWin.once("ready-to-show", () => {
            this.adsorbWin.show();
        });
        if (isDevelopment) {
            this.adsorbWin.loadURL(path.join("http://localhost:7758", hash));
        } else {
            this.adsorbWin.loadFile(path.join(__dirname, "../platform/index.html"), {
                hash,
            });
        }

        /*========================================>( 监听事件 )<========================================*/
        //监听父窗口移动ing
        const handleWindowMove = () => {
            //1.如果已经是磁吸状态，则子窗口跟随父窗口移动，如果超出桌面，则内吸附
            if (adsorption) {
                //父窗口移动时，子窗口跟随移动即可
                //上一时刻父窗口位置
                const { x: parentX, y: parentY } = momentParentBounds
                const parentBounds = window.getBounds()
                //移动时父窗口位置
                const { x: parentX2, y: parentY2 } = parentBounds
                const { x, y } = this.adsorbWin.getBounds()
                //移动时子窗口位置
                const finalX = x + parentX2 - parentX
                const finalY = y + parentY2 - parentY
                this.adsorbWin.setPosition(finalX, finalY);
                //保存为上一时刻父窗口位置
                momentParentBounds = parentBounds
            }
            //2.如果是非磁吸状态，则保持在原位置
        }

        //监听父窗口移动结束
        const handleWindowMoved = () => {
            momentParentBounds = window.getBounds()
            //1.如果是吸附状态，如果判断超限，工具箱进行左/右吸附
            if (adsorption == 'r' && isRightOutOfDesk()) {
                //右吸附超出界外，右内吸附
                absorbInnerRight()
            }
            if (adsorption == 'l' && isLeftOutOfDesk()) {
                //左吸附超出界外，左内吸附
                absorbInnerLeft()
            }
        }

        //监听父窗口最大化
        const handleWindowMaximize = () => {
            //1.如果是外贴，改为这个方向的内贴
            //2.如果是内贴，继续这个方向内贴
            //3.如果是自由，右侧内贴
            momentParentBounds = window.getBounds()
            if (adsorption == 'l' || adsorption == 'nl') {
                absorbInnerLeft()
                return
            }
            absorbInnerRight()
        }

        //监听父窗口退出最大化
        const handleWindowUnmaximize = () => {
            //如果是左内贴，该外左外贴，其余情况全部右外贴
            momentParentBounds = window.getBounds()
            if (adsorption == 'nl') {
                absorbLeft()
                return
            }
            absorbRight()
        }

        //监听父窗口调整大小
        const handleWindowResized = () => {
            momentParentBounds = window.getBounds()
            //如果是内贴继续这个方向内贴
            if (adsorption == 'nl') {
                absorbInnerLeft()
                return
            }
            if (adsorption == 'nr') {
                absorbInnerRight()
                return
            }
            //如果是外贴，判断内外贴
            if (adsorption == 'l') {
                if (isLeftOutOfDesk()) {
                    absorbInnerLeft()
                    return
                }
                absorbLeft()
                return
            }
            if (adsorption == 'r') {
                if (isRightOutOfDesk()) {
                    absorbInnerRight()
                    return
                }
                absorbRight()
                return
            }
        }

        //监听子窗口移动
        const handleSubWinMoved = () => {
            const { x: parentX, y: parentY, width: parentWidth } = momentParentBounds
            const { x, width } = this.adsorbWin.getBounds()
            const parentRightX = parentX + parentWidth
            const rightX = x + width
            if (parentRightX >= x && parentRightX <= rightX) {
                //右吸附
                if (isRightOutOfDesk()) {
                    absorbInnerRight()
                } else {
                    absorbRight()
                }
                return
            }
            if (parentX >= x && parentX <= rightX) {
                //左吸附
                if (isLeftOutOfDesk()) {
                    absorbInnerLeft()
                } else {
                    absorbLeft()
                }
                return
            }
            adsorption = undefined
        }


        //监听父窗口移动ing
        window.on('move', handleWindowMove);

        //监听父窗口移动结束
        window.on('moved', handleWindowMoved);

        //监听父窗口最大化
        window.on('maximize', handleWindowMaximize);

        //监听父窗口退出最大化
        window.on('unmaximize', handleWindowUnmaximize);

        //监听父窗口调整大小
        window.on('resized', handleWindowResized);

        //监听子窗口移动，实现横向磁吸
        this.adsorbWin.on('moved', handleSubWinMoved);

        //父子窗口销毁时的监听
        this.adsorbWin.on('closed', () => {
            window.removeListener('move', handleWindowMove);
            window.removeListener('moved', handleWindowMoved);
            window.removeListener('maximize', handleWindowMaximize);
            window.removeListener('unmaximize', handleWindowUnmaximize);
            window.removeListener('resized', handleWindowResized);
        })







    }
}